<template>
  <div>
    <el-form
      class="query-form"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      size="small"
      label-width=""
    >
      <el-form-item class="big-query" label="项目" prop="appid">
        <el-select v-model="queryParams.appid"
          multiple
          filterable
          placeholder="请选择"
          @change="handleQuery">
          <el-option v-for="item in permission_projectList" :value="item.app_id" :label="item.name" :key="item.app_id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="big-query" label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键词"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
          @clear="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery"
          >搜索</el-button
        >
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div>
      <el-button type="primary"
        icon="el-icon-plus"
        style="margin-bottom:10px"
        size="small"
        @click="handleAdd">新增</el-button>
    </div>
    <el-table v-loading="loading"
      :data="dataList">
      <el-table-column label="功能名称"
        min-width="120"
        prop="fn_name" />
      <el-table-column label="功能ID"
        min-width="120"
        prop="fn_id">
      </el-table-column>
      <el-table-column label="所属项目"
        min-width="120"
        prop="appid">
        <template slot-scope="scope">
          {{scope.row.appid|translateToName(permission_projectList,'app_id','name')}}
        </template>
      </el-table-column>
      <el-table-column label="消费公式"
        min-width="200"

        prop="icon">
        <template slot-scope="scope">
          {{ scope.row.fn_num+scope.row.fn_unit_name }} = {{ scope.row.price+scope.row.price_unit_name }}
        </template>
      </el-table-column>
      <el-table-column label="图标"
        min-width="100"
        prop="icon">
        <template slot-scope="scope">
          <el-image slot="reference"
            style="height: 36px;cursor: pointer;"
            :src="scope.row.icon"
            fit="contain"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作"
        width="120"
        fixed="right">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="handleUpdate(scope.row)">修改</el-button>

        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList" />
    <el-dialog title="计费权益"
      :visible.sync="open"
      width="680px"
      :close-on-click-modal="false"
      @closed="onDialogClosed">
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="90px">
        <el-form-item label="项目" prop="appid">
          <el-select v-model="form.appid" class="width-250">
            <el-option v-for="item in permission_projectList" :value="item.app_id" :label="item.name" :key="item.app_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="功能" prop="fn_id">
          <el-select v-model="form.fn_id" class="width-250">
            <el-option v-for="item in fnList" :value="item.fn_id" :label="item.name+'（ID:'+item.fn_id+'）'" :key="item.fn_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消费公式" required>
          <el-col :span="5">
            <el-form-item prop="fn_num">
              <el-input-number v-model="form.fn_num" :min="1" :precision="0" controls-position="right" style="width: 100px;"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item prop="fn_unit_name">
              <el-select v-model="form.fn_unit_name" style="width: 100px;">
                <el-option value="次" label="次"></el-option>
                <el-option value="页" label="页"></el-option>
                <el-option value="字符" label="字符"></el-option>
                <el-option value="token" label="token"></el-option>
                <el-option value="问题" label="问题"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">消耗</el-col>
          <el-col :span="5">
            <el-form-item prop="price">
              <el-input-number v-model="form.price" :min="1" :precision="0" controls-position="right" style="width: 100px;"></el-input-number>
            </el-form-item>
            </el-col>
          <el-col :span="5">
            <el-form-item prop="price_unit_id">
              <el-select v-model="form.price_unit_id" style="width: 160px;">
                <el-option v-for="item in chargeUnitList" :value="item.unit_id" :label="item.internal_name" :key="item.unit_id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="功能图标"
          prop="icon">
          <ImgUpload v-model="form.icon"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary"
          :loading="saveLoading"
          @click="submitForm">确 认</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import ImgUpload from '@/components/img-upload'
import { getFormulaList, getFnList, addFormula } from '@/api/addition'
import { getChargeUnit } from '@/api/system'
export default {
  name: 'ChargeFormula',
  components: {
    ImgUpload
  },
  data () {
    return {
      loading: false,
      saveLoading: false,
      chargeUnitList: [],
      fnList: [],
      queryParams: {
        page: 1,
        page_size: 10,
        keyword: '',
        appid: []
      },
      dataList: [],
      total: 0,
      open: false,
      form: {
        id: null,
        appid: null,
        fn_id: null,
        fn_unit_name: '次',
        fn_num: undefined,
        price: undefined,
        price_unit_id: '',
        icon: ''
      },
      rules: {
        appid: [{ required: true, message: '项目不能为空', trigger: 'blur' }],
        fn_id: [{ required: true, message: '功能不能为空', trigger: 'blur' }],
        fn_num: [{ required: true, message: '不能为空', trigger: 'blur' }],
        fn_unit_name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        price: [{ required: true, message: '不能为空', trigger: 'blur' }],
        price_unit_id: [{ required: true, message: '不能为空', trigger: 'blur' }],
        icon: [
          { required: true, message: '图标不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters('zcsd-core', ['permission_projectList'])
  },
  created () {
    const appid = this.permission_projectList?.[0]?.app_id
    this.queryParams.appid = [ appid ]
    getChargeUnit().then(res => {
      this.chargeUnitList = res.data.list
    })
    getFnList({ page: 1, page_size: 100 }).then(res => {
      this.fnList = res.data.list
    })
    this.getList()
  },
  methods: {
    // 点击搜索
    handleQuery () {
      this.queryParams.page = 1
      this.getList()
    },
    // 重置
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleAdd () {
      this.open = true
    },
    getList () {
      this.loading = true
      const params = Object.assign({}, this.queryParams)
      if (params.appid?.length) {
        params.appid = params.appid.join(',')
      } else {
        delete params.appid
      }
      getFormulaList(params).then(res => {
        const data = res.data || {}
        this.dataList = data.list
        this.total = data.count
      }).finally(() => {
        this.loading = false
      })
    },
    handleUpdate (row) {
      this.open = true

      this.$nextTick(() => {
        Object.keys(this.form).forEach(key => {
          this.form[key] = row[key]
        })
      })
    },
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.saveLoading = true
          addFormula(this.form, this.form.id ? 'put' : 'post').then(() => {
            this.getList()
            this.open = false
            this.msgSuccess(this.form.id ? '修改成功' : '创建成功')
          }).finally(() => {
            this.saveLoading = false
          })
        }
      })
    },
    onDialogClosed () {
      this.resetForm('form')
      this.form.id = null
    }
  }
}
</script>
